package com.minggo.charmword.adapter;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.minggo.charmword.R;
import com.minggo.charmword.model.ReviewHistory;
import com.minggo.charmword.util.MinggoDate;

/**
 * 锁屏历史记录
 * 
 * @author minggo
 * @date 2014-9-1 下午2:38:54
 */
public class LockHistoryAdapter extends BaseAdapter {

	private Context context;
	private List<ReviewHistory> histotyList;
	private Date date = new Date();
	private long dateTime = date.getTime();

	public LockHistoryAdapter(Context context, List<ReviewHistory> histotyList) {
		this.context = context;
		this.histotyList = histotyList;
	}

	@Override
	public int getCount() {
		return histotyList.size();
	}

	@Override
	public Object getItem(int position) {
		return histotyList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_history, null);
			viewHolder.dateTv = (TextView) convertView.findViewById(R.id.tv_history_time);
			viewHolder.lockCountTv = (TextView) convertView.findViewById(R.id.tv_lock_count);
			viewHolder.isReviewTvl = (TextView) convertView.findViewById(R.id.tv_is_review);
			viewHolder.beyondTv = (TextView) convertView.findViewById(R.id.tv_lock_count_beyong);
			viewHolder.todayLo = convertView.findViewById(R.id.lo_today_loack);
			viewHolder.shareLo = convertView.findViewById(R.id.lo_history_share);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (dateTime - histotyList.get(position).dateTime < 24 * 60 * 60 * 1000) {
			viewHolder.todayLo.setVisibility(View.VISIBLE);
			viewHolder.shareLo.setVisibility(View.VISIBLE);
			viewHolder.lockCountTv.setText(histotyList.get(position).contiueDays + "");

			viewHolder.dateTv.setText("今天");
			viewHolder.dateTv.setTextColor(context.getResources().getColor(R.color.navi_index));
		} else {
			viewHolder.todayLo.setVisibility(View.GONE);
			viewHolder.shareLo.setVisibility(View.GONE);
			viewHolder.dateTv.setTextColor(context.getResources().getColor(R.color.text_light));
			if ((dateTime - histotyList.get(position).dateTime >= 24 * 60 * 60 * 1000)
					&& (dateTime - histotyList.get(position).dateTime < 48 * 60 * 60 * 1000)) {
				viewHolder.dateTv.setText("昨天");
			} else {
				viewHolder.dateTv.setText(MinggoDate.dateFormat0(new Date(histotyList.get(position).dateTime)));
			}
		}

		viewHolder.beyondTv.setText("5");

		viewHolder.lockCountTv.setText(histotyList.get(position).lockCount + "");

		if (histotyList.get(position).isReview == 0) {
			viewHolder.isReviewTvl.setText("没有复习");
		} else {
			viewHolder.isReviewTvl.setText("已经复习");
		}

		return convertView;
	}

	private class ViewHolder {
		TextView dateTv;
		TextView lockCountTv;
		TextView isReviewTvl;
		TextView beyondTv;
		View shareLo;
		View todayLo;
	}
}
